<?php
class View_Object{
	/**
	 * вывод всех объектов
	 */
	public static function showAllObject($arrSortObject){
		$objSession = User_Session::instance();
		
		foreach ($arrSortObject as $arrObject){
			echo '<h1>';
			switch ($arrObject[0]['type']){
				case 1: echo '<a class="none" href="' . W_ABSPATH . 'houses">Дома из бруса</a> → '; break;
				case 2: echo '<a class="none" href="' . W_ABSPATH . 'bathhouse">Бани из бруса</a> → '; break;
			}
			echo View_Object::getTypeName($arrObject[0]['type']) . '</h1>';
			
			foreach ($arrObject as $arrOneObject){
				echo '<div>';
					self::showOneObjectInList($arrOneObject);
				echo '</div>';
			}
			echo '<div class="clearWithoutClear"></div>';
		}
		if ($objSession->isLoggedIn())
			echo '<p class="editPage"><a class="admin" href="' . W_ABSPATH . 'object/add">Добавить объект</a></p>';
	}
	
	/**
	 * вывод одного объекта в списке
	 */
	public static function showOneObjectInList($arrObject){
		$objSession = User_Session::instance();
		echo '<div class="greyBlock oneObject" id="object_' . $arrObject['ID_object'] . '">';
			echo '<h3><a class="head" href="' . W_ABSPATH . 'object/' . $arrObject['ID_object'] . '">' . $arrObject['name'] . '</a></h3>';
			echo '<a class="none" href="' . W_ABSPATH . 'object/' . $arrObject['ID_object'] . '"><img src="' . W_IMAGES . 'upload/thumb/' . $arrObject['imageName'] . '" /></a>';
			
			if ($arrObject['date_first'] != '0000-00-00')
				echo '<p>Начало строительства: ' . Service_Date::getCaseDateForUser($arrObject['date_first'], 3, 2, false, true) . '</p>';
			if ($arrObject['date_last'] != '0000-00-00')
				echo '<p>Окончание строительства: ' . Service_Date::getCaseDateForUser($arrObject['date_last'], 3, 2, true, true) . '</p>';
			if ($arrObject['price'] != 0)
				echo '<p>Стоимость: ' . Service_Currency::getShortPrice($arrObject['price']) . '' . Service_Currency::getCurrencyForUser($arrObject['currency']) . '</p>';
			
			if (!empty($arrObject['annotation'])){
				//echo '<p><a class="js" href="#" onClick="$(\'#annotation_' .$arrObject['ID_object'] . '\').slideToggle(); return false;">краткое описание</a></p>';
				echo '<p class="annotation" id="annotation_' .$arrObject['ID_object'] . '" >' . $arrObject['annotation'] . '</p>';
			}
			echo '<div class="clearWithoutClear"></div>';
			echo '<p class="allInfo"><a href="' . W_ABSPATH . 'object/' . $arrObject['ID_object'] . '">подробнее →</a></p>';
			if ($objSession->isLoggedIn()){
				echo '<p class="editPage">';
					echo '<a class="admin" href="' . W_ABSPATH . 'object/edit/' . $arrObject['ID_object'] . '">Редактировать&nbsp;информацию</a> ';
					echo '<a class="admin" href="' . W_ABSPATH . 'object/editPhotos/' . $arrObject['ID_object'] . '">Редактировать&nbsp;фоторграфии</a> ';
					echo '<a class="admin" href="javascript:object.deleteObject(' . $arrObject['ID_object'] . ');">Удалить&nbsp;объект</a>';
				echo '</p>';
			}			
		echo '</div>';
	}
	
	/**
	 * полный вывод объекта
	 *
	 */
	public static function showOneObject($arrObject, $arrImages){
		$objSession = User_Session::instance();
		echo $arrObject['annotation'] . '<br/>';
		echo $arrObject['text'];
		echo '<br/>';
		echo '<ul class="empty">';
			if (!empty($arrObject['price']))
				echo '<li><b>Цена:</b> ' . Service_Currency::getShortPrice($arrObject['price']) . ' ' . Service_Currency::getCurrencyForUser($arrObject['currency']) . '</li>';
			if ($arrObject['date_first'] != '0000-00-00')
				echo '<li><b>Начало строительства:</b> ' . Service_Date::getCaseDateForUser($arrObject['date_first'], 3, 2, false, true) . '</li>';
			if ($arrObject['date_last'] != '0000-00-00')
				echo '<li><b>Окончание строительства:</b> ' . Service_Date::getCaseDateForUser($arrObject['date_last'], 3, 2, false, true) . '</li>';
		echo '</ul>';
		
		
		View_Gallery::showThumb($arrImages, 1, $arrObject['ID_object'], false, true);
		
		echo '<div class="clearWithoutClear"></div>';

		if ($objSession->isLoggedIn()){
			echo '<p class="editPage">';
				echo '<a class="admin" href="' . W_ABSPATH . 'object/edit/' . $arrObject['ID_object'] . '">Редактировать информацию</a>';
				echo '<a class="admin" href="' . W_ABSPATH . 'object/editPhotos/' . $arrObject['ID_object'] . '">Редактировать фоторграфии</a>';
			echo '</p>';
		}
	}
	
	/**
	 * вывод страницы сущности
	 */
	public static function showObjectTypePage($type, $arrImages){
		$objSession = User_Session::instance();
		View_Gallery::showThumb($arrImages, 2, View_Object::getIdByType($type), false, true);
		
		echo '<div class="clearWithoutClear"></div>';
		
		if ($objSession->isLoggedIn()){
			echo '<p class="editPage">';
				echo '<a class="admin" href="' . W_ABSPATH . 'object/editTypePhotos/' . View_Object::getIdByType($type) . '">Редактировать фоторграфии</a>';
			echo '</p>';
		}
	}
	
	/**
	 * форма добавления/редактирования объекта
	 *
	 */
	public static function showObjectEditForm($arrObject){    
			echo '<form method="post" action="">';
			echo '<div class="box_trext_form">
							<label>Назвние</label>
							<div class="info_label">
								<input type="text" value="' . (isset($arrObject['name']) ? $arrObject['name'] : '') . '" name="name" class="tx" />
							</div>
						</div>';
						
			echo '<div class="box_trext_form">
							<label>Краткое описание</label>
							<div class="info_label">
								<textarea name="annotation" rows="5" cols="47">' . (isset($arrObject['annotation']) ? $arrObject['annotation'] : '') . '</textarea>
							</div>
						</div>';
						
			echo '<div class="box_trext_form">
							<label>Дополнительные сведения</label>
							<div class="info_label">
								<textarea name="text" id="wysiwyg" rows="5" cols="47">' . (isset($arrObject['text']) ? $arrObject['text'] : '') . '</textarea>
							</div>
						</div>';
						if($a == 2);
  			if (empty($arrObject) || $arrObject['type'] == 1 || $arrObject['type'] == 2){
  				
  				echo '<div class="box_trext_form">
  								<label>Раздел</label>
  								<div class="info_label">
  									<select name="type" >
  										<option value="1" ' . ((isset($arrObject['type']) AND $arrObject['type'] == 1) ? 'selected="selected"' : '') . '>Дома из бруса</option>
  										<option value="2" ' . ((isset($arrObject['type']) AND $arrObject['type'] == 2) ? 'selected="selected"' : '') . '>Бани из бруса</option>
  									</select>
  								</div>
  							</div>';
  						
  				echo '<div class="box_trext_form">
  								<label>Стоимость</label>
  								<div class="info_label">
  									<input type="text" style="width: 100px;" value="' . ((isset($arrObject['price']) and !empty($arrObject['price'])) ? floatval($arrObject['price']) : '') . '" name="price" class="" />
  									<select name="currency" style="width: 50px;">
  										<option value="1">руб.</option>
  										<option value="2" ' . ((isset($arrObject['currency']) AND $arrObject['currency'] == 1) ? 'checked="checked"' : '') . '>$</option>
  										<option value="3" ' . ((isset($arrObject['currency']) AND $arrObject['currency'] == 2) ? 'checked="checked"' : '') . '>€</option>
  									</select>
  								</div>
  							</div>';
  							
  				echo '<div class="box_trext_form">
  								<label>Сроки</label>
  								<div class="info_label">
  									<input style="width: 70px;" type="text" value="' . ((isset($arrObject['date_first']) and $arrObject['date_first'] != '0000-00-00') ? date("d.m.Y", strtotime($arrObject['date_first'])) : '') . '" name="date_first" id="date_first" /> —
  									<input style="width: 70px;" type="text" value="' . ((isset($arrObject['date_last']) and $arrObject['date_last'] != '0000-00-00') ? date("d.m.Y", strtotime($arrObject['date_last'])) : '') . '" name="date_last" id="date_last" />
  									<p>Например: 10.09.2009 - 12.07.2012</p>
  								</div>
  							</div>';        
          
  			}
  			else{
  			  echo '<input name="type" type="hidden" value="3"/>';
  				echo '<input name="price" type="hidden" value=""/>';
  				echo '<input name="currency" type="hidden" value="1"/>';
  				echo '<input name="date_first" type="hidden" value=""/>';
  				echo '<input name="date_last" type="hidden" value=""/>';
  			}
			
			echo '<input type="hidden" name="ID_object" value="' . (isset($arrObject['ID_object']) ? $arrObject['ID_object'] : '') . '"  />';
			echo '<input type="image" class="mainOtpravit" value="Сохранить" src="' . W_IMAGES . 'main_otpravit.gif" />';
		echo '</form>';
	}
	
	/**
	 * вывод объектов в колонку
	 *
	 */
	public static function showObjectIncolumn($arrObjects){
		foreach ($arrObjects as $object){
			$object['annotation'] = Service_String::cutString($object['annotation'], 200);
			echo '<div>';
				echo '<h3><a href="' . W_ABSPATH . 'object/' . $object['ID_object'] . '">' . $object['name'] . '</a></h3>';
				echo '<img src="' . W_IMAGES . 'upload/thumb/' . $object['imageName'] . '" />';
				echo '<p>' . $object['annotation'] . '</p>';
			echo '</div>';
			echo '<div class="clearWithoutClear"></div>';
		}
	}
	
	/**
	 * получаем Название фазы
	 */
	public static function getTypeName($type){
		//echo $type;
		switch ($type){
			case 1:
			case 3: return 'Дома из бруса';
			case 2:
			case 4: return 'Бани из бруса';
		}
	}
	
	public static function getIdByType($type){
		switch ($type){
			case 'houses': return 1;
			case 'bathhouse': return 2;
		}		
	}
	
	public static function getNameByType($type){
		return self::getTypeName(self::getIdByType($type));
	}
	
	/**
	 * получаем Название фазы
	 */
	public static function getUrlNameByType($type){
		switch ($type){
			case 1: return 'houses';
			case 2: return 'bathhouse';
		}
	}
}
?>